/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package formularios;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import modelo.ModeloTabela;
import utilizador_bd.ConectBD;

/**
 *
 * @author Daniel
 */
public class Funcoes extends javax.swing.JFrame {

      ConectBD conect = new ConectBD();
private String SQL;
    public Funcoes() {
        initComponents();
         conect.conexao();
         preencherTabela("select * from funcao order by id_funcao");

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu1 = new javax.swing.JMenu();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1idfuncao = new javax.swing.JTextField();
        jTextField2descri = new javax.swing.JTextField();
        jTextField3salar = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField4h_entrada = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextField5h_saida = new javax.swing.JTextField();
        jButton1novo = new javax.swing.JButton();
        jButton2guardar = new javax.swing.JButton();
        jButton3edit = new javax.swing.JButton();
        jButton1eliminar = new javax.swing.JButton();
        jButton1logout = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2funcao = new javax.swing.JTable();
        jLabel8 = new javax.swing.JLabel();
        jButton2ultimo = new javax.swing.JButton();
        jButton1anterior = new javax.swing.JButton();
        jButton2proximo = new javax.swing.JButton();

        jMenu1.setText("jMenu1");

        jLabel5.setText("jLabel5");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jLabel7.setText("jLabel7");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Id_Função");

        jLabel2.setText("Descrição");

        jLabel3.setText("Salario");

        jTextField1idfuncao.setEditable(false);
        jTextField1idfuncao.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextField1idfuncao.setEnabled(false);

        jTextField2descri.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextField2descri.setEnabled(false);

        jTextField3salar.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextField3salar.setEnabled(false);

        jLabel4.setText("Hora Entrada");

        jTextField4h_entrada.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextField4h_entrada.setEnabled(false);

        jLabel6.setText("Hora Saída");

        jTextField5h_saida.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextField5h_saida.setEnabled(false);

        jButton1novo.setText("Novo");
        jButton1novo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1novoActionPerformed(evt);
            }
        });

        jButton2guardar.setText("Guardar");
        jButton2guardar.setEnabled(false);
        jButton2guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2guardarActionPerformed(evt);
            }
        });

        jButton3edit.setText("Editar");
        jButton3edit.setEnabled(false);
        jButton3edit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3editActionPerformed(evt);
            }
        });

        jButton1eliminar.setText("Eliminar");
        jButton1eliminar.setEnabled(false);
        jButton1eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1eliminarActionPerformed(evt);
            }
        });

        jButton1logout.setText("Logout");
        jButton1logout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1logoutActionPerformed(evt);
            }
        });

        jTable2funcao.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2funcao);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(255, 102, 102));
        jLabel8.setText("Inserir Função");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(jTextField3salar))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(jTextField2descri))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(jTextField1idfuncao, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(95, 95, 95)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField5h_saida, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField4h_entrada, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 519, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jButton1novo)
                                .addGap(18, 18, 18)
                                .addComponent(jButton2guardar)
                                .addGap(18, 18, 18)
                                .addComponent(jButton3edit)
                                .addGap(18, 18, 18)
                                .addComponent(jButton1eliminar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton1logout))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(171, 171, 171)
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTextField1idfuncao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(21, 21, 21)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTextField2descri, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addGap(37, 37, 37)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jTextField4h_entrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(21, 21, 21)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(jTextField5h_saida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField3salar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1novo)
                    .addComponent(jButton2guardar)
                    .addComponent(jButton3edit)
                    .addComponent(jButton1eliminar)
                    .addComponent(jButton1logout))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton2ultimo.setText("Último");
        jButton2ultimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ultimoActionPerformed(evt);
            }
        });

        jButton1anterior.setText("Anterior");

        jButton2proximo.setText("Próximo");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(226, 226, 226)
                .addComponent(jButton2ultimo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1anterior)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2proximo)
                .addContainerGap(142, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2ultimo)
                    .addComponent(jButton1anterior)
                    .addComponent(jButton2proximo))
                .addContainerGap(54, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2guardarActionPerformed

          try {
               PreparedStatement pst;
    
          pst = conect.con.prepareStatement("insert into funcao (descricao,salario,horasaida,horaentrada)values (?,?,?,?)");
                     
            pst.setString(1, jTextField2descri.getText());
            pst.setString(2, jTextField3salar.getText());
               pst.setString(3, jTextField5h_saida.getText());
            pst.setString(4, jTextField4h_entrada.getText());
          pst.executeUpdate();
            JOptionPane.showMessageDialog(rootPane, "Salvo com sucesso!");

          
          } catch (SQLException ex) {
JOptionPane.showMessageDialog(rootPane, "Erro na inserção!\n Erro:" +ex);          }
jTextField2descri.setEnabled(false);
            jTextField3salar.setEnabled(false);
            jTextField4h_entrada.setEnabled(false);
            jTextField5h_saida.setEnabled(false);
            jButton2guardar.setEnabled(false);
            jButton1eliminar.setEnabled(false);
            jButton3edit.setEnabled(false);
            jButton1novo.setEnabled(true);
            
    }//GEN-LAST:event_jButton2guardarActionPerformed


    private void jButton1novoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1novoActionPerformed
            jTextField2descri.setEnabled(true);
            jTextField3salar.setEnabled(true);
            jTextField4h_entrada.setEnabled(true);
            jTextField5h_saida.setEnabled(true);
            jButton2guardar.setEnabled(true);
            jButton1eliminar.setEnabled(true);
            jButton3edit.setEnabled(true);
            jButton1novo.setEnabled(false);

    }//GEN-LAST:event_jButton1novoActionPerformed

    private void jButton1logoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1logoutActionPerformed
int resposta ;
        
        resposta=JOptionPane.showConfirmDialog(null, "Quer mesmo sair do programa?");       

       if (resposta == JOptionPane.YES_OPTION) {

dispose();



} else {


}      }//GEN-LAST:event_jButton1logoutActionPerformed

    private void jButton1eliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1eliminarActionPerformed
 try {
            PreparedStatement pft;
            pft = conect.con.prepareStatement("delete from funcao where descricao='" +jTextField2descri.getText()+"'");
            pft.executeUpdate();
            
            JOptionPane.showMessageDialog(rootPane, "Eliminado com sucesso:");
        } catch (SQLException ex) {
           JOptionPane.showMessageDialog(rootPane, "Erro na inserção!\n Erro:" +ex.getMessage());
        }
        
        
        
        jTextField2descri.setEnabled(false);
            jTextField3salar.setEnabled(false);
            jTextField4h_entrada.setEnabled(false);
            jTextField5h_saida.setEnabled(false);
            jButton2guardar.setEnabled(false);
            jButton1eliminar.setEnabled(false);
            jButton3edit.setEnabled(false);
            jButton1novo.setEnabled(true);        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1eliminarActionPerformed

    private void jButton2ultimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ultimoActionPerformed
conect.executaSQL("select *  from funcao");
          try {
              conect.re.last();
              
              jTextField1idfuncao.setText(String.valueOf(conect.re.getInt("id_funcao")));
              jTextField2descri.setText(conect.re.getString("descricao"));
              jTextField3salar.setText(String.valueOf(conect.re.getInt("salario")));
              jTextField4h_entrada.setText(String.valueOf(conect.re.getTime("horaentrada")));
              jTextField5h_saida.setText(String.valueOf(conect.re.getTime("horasaida")));

          } catch (SQLException ex) {
           JOptionPane.showMessageDialog(rootPane, "Erro no anterior!\n Erro:" +ex.getMessage());
          }


    }//GEN-LAST:event_jButton2ultimoActionPerformed

    private void jButton3editActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3editActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton3editActionPerformed

    
    
    /**
     * @param args the command line arguments
     */
    
    public void preencherTabela(String SQL){
        ArrayList dados = new ArrayList();
        
        String [] Colunas = new String[]{"ID", "descrição", "Salário", "Hora Entrada", "Hora Saída"};
        
        conect.executaSQL(SQL);
          try {
              conect.re.first();
              
                do{
         dados.add(new Object[]{conect.re.getInt("id_funcao"),conect.re.getString("descricao"),conect.re.getInt("salario"),conect.re.getTime("horaentrada"),conect.re.getTime("horasaida")});
     } while(conect.re.next());
     
          } catch (SQLException ex) {
           JOptionPane.showMessageDialog(null, "Erro no array!\n Erro:" +ex.getMessage());
          }
          
     ModeloTabela modelo = new ModeloTabela(dados, Colunas);
     
     jTable2funcao.setModel(modelo);
     jTable2funcao.getColumnModel().getColumn(0).setPreferredWidth(110);
     jTable2funcao.getColumnModel().getColumn(0).setResizable(false);
     jTable2funcao.getColumnModel().getColumn(1).setResizable(false);
     jTable2funcao.getColumnModel().getColumn(1).setPreferredWidth(110);
     jTable2funcao.getColumnModel().getColumn(2).setResizable(false);
     jTable2funcao.getColumnModel().getColumn(2).setPreferredWidth(110);
     jTable2funcao.getColumnModel().getColumn(3).setResizable(false);
     jTable2funcao.getColumnModel().getColumn(3).setPreferredWidth(110);
     jTable2funcao.getColumnModel().getColumn(4).setResizable(false);
     jTable2funcao.getColumnModel().getColumn(4).setPreferredWidth(110);

     jTable2funcao.getTableHeader().setReorderingAllowed(false);
     jTable2funcao.setAutoResizeMode(jTable2funcao.AUTO_RESIZE_OFF);
     jTable2funcao.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }
    
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Funcoes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Funcoes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Funcoes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Funcoes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Funcoes().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1anterior;
    private javax.swing.JButton jButton1eliminar;
    private javax.swing.JButton jButton1logout;
    private javax.swing.JButton jButton1novo;
    private javax.swing.JButton jButton2guardar;
    private javax.swing.JButton jButton2proximo;
    private javax.swing.JButton jButton2ultimo;
    private javax.swing.JButton jButton3edit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2funcao;
    private javax.swing.JTextField jTextField1idfuncao;
    private javax.swing.JTextField jTextField2descri;
    private javax.swing.JTextField jTextField3salar;
    private javax.swing.JTextField jTextField4h_entrada;
    private javax.swing.JTextField jTextField5h_saida;
    // End of variables declaration//GEN-END:variables
}
